#-*- coding:utf-8 -*-
import os
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
from asset_widget.AssetDB import AssetDB


class Search(QWidget):

    def __init__(self):
        super(Search,self).__init__()
        self.setStyleSheet('background-color:rgb(75,75,75);color:rgb(220,220,220)')
        #全局变量
        self.list_data={}#该数据缓存所有查询到的结果，并以{'asset_name':(db_data)}形式对应起来

        self.setWindowTitle("搜索资产")
        self.mainLayout=QHBoxLayout()

        self.left_layout=QVBoxLayout()
        self.search_layout=QHBoxLayout()
        self.search_comb=QComboBox()
        self.search_comb.addItems(['资产名称查询','标签查询','人名查询'])
        # self.search_label=QLabel('关键字查询:')
        self.search_edit=QLineEdit()


        self.search_layout.addWidget(self.search_comb)
        self.search_layout.addWidget(self.search_edit)

        self.content_text=QTextBrowser()

        self.btn_layout=QHBoxLayout()
        self.OK_btn=QPushButton('查询')
        self.OK_btn.setMinimumSize(50, 50)
        self.OK_btn.clicked.connect(self.__search)
        self.NO_btn=QPushButton('取消')
        self.NO_btn.setMinimumSize(50, 50)
        self.NO_btn.clicked.connect(self.__closeWindow)
        self.btn_layout.addWidget(self.OK_btn)
        self.btn_layout.addWidget(self.NO_btn)

        self.left_layout.addLayout(self.search_layout)
        self.left_layout.addWidget(self.content_text)
        self.left_layout.addLayout(self.btn_layout)

        self.right_layout=QVBoxLayout()
        self.search_result_label=QLabel('查询结果:')
        self.search_result_list=QListWidget()
        #添加右击菜单栏
        self.search_result_list.setContextMenuPolicy(Qt.CustomContextMenu)
        self.search_result_list.customContextMenuRequested.connect(self.__rightMenu)
        #添加单击事件
        self.search_result_list.clicked.connect(self.__showInfo)



        self.right_layout.addWidget(self.search_result_label)
        self.right_layout.addWidget(self.search_result_list)



        self.mainLayout.addLayout(self.left_layout)
        self.mainLayout.addLayout(self.right_layout)

        self.setLayout(self.mainLayout)

        #数据库
        self.submit_path = '//192.168.1.247/effects/AssetsLib'  # 工程存放路径
        self.db_path=os.path.join(self.submit_path,'db')  #数据库路径
        self.table_name='asset_lib'
        self.db=AssetDB(self.db_path,self.table_name)



    def __rightMenu(self):
        self.menu=QMenu()
        self.openAction=QAction(u'打开目录',self)
        self.openAction.triggered.connect(self.__openDir)
        self.menu.addAction(self.openAction)

        self.menu.exec_(QCursor.pos())




    def __search(self):
        search_type_list=['asset_name','tag','name']
        search_type=search_type_list[self.search_comb.currentIndex()]
        tag=self.search_edit.text()

        if tag:
            self.__clearWindows()#先清空所有窗口
            asset_name_list=[]
            self.list_data = {}#清空缓存数据
            result= self.db.searchData(search_type,tag)
            if result:
                for i in result:#type_list=['id','asset_name','content','tag','name','asset_path']
                    asset_name_list.append(i[1])
                    self.list_data[i[1]]=i

                self.search_result_list.clear()
                self.search_result_list.addItems(asset_name_list)

            else:
                self.content_text.setText('很遗憾，没有类似资产,请多多上传')

    def __showInfo(self):
        currentItem=self.search_result_list.currentItem()
        if currentItem:
            current_asset_name=currentItem.text()
            info=self.list_data[current_asset_name]
            asset_name=current_asset_name
            name=info[4]
            content=info[2]
            text_mode=u'资产名称:{}\n\n上传人员:{}\n\n资产说明:{}'.format(asset_name,name,content)

            self.content_text.setText(text_mode)


    def __openDir(self):

        currentItem=self.search_result_list.currentItem()
        if currentItem:
            current_asset_name=currentItem.text()
            asset_path=self.list_data[current_asset_name][5]
            path=asset_path.replace('/','\\')
            os.popen(r"explorer.exe %s"%path)
            # os.startfile(asset_path)

    def __clearWindows(self):
        '''
        先清空窗口
        :return:
        '''
        self.content_text.clear()
        self.search_result_list.clear()

    def __closeWindow(self):
        self.close()

if __name__ == '__main__':
    app=QApplication()
    d=Search()
    d.show()
    sys.exit(app.exec_())

